package GUI;

import Types.Complex;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author Cliff Lachet
 */
public class Plot {

    /**
     * Creates a standard plot of a double valued data series, containing only
     * 1 series of data (1 line).
     * @param data
     * @param chartName
     * @param dataLabel
     * @param xAxis
     * @param yAxis
     * @return
     */
    public static ChartPanel standardPlot(double[] data, String chartName,
            String dataLabel, String xAxis, String yAxis) {
        XYSeries series1 = new XYSeries(dataLabel);
        for (int i = 0; i < data.length; i++) {
            series1.add(i * 0.01, data[i]);
        }

        XYDataset plotData = new XYSeriesCollection(series1);

        JFreeChart chart = ChartFactory.createXYLineChart(chartName, xAxis, yAxis,
                plotData, PlotOrientation.VERTICAL, false,
                false, false);

        NumberAxis axisX = new NumberAxis(xAxis);
        axisX.setAutoRange(true);

        NumberAxis axisY = new NumberAxis(yAxis);
        axisY.setAutoRange(true);

        ChartPanel panel = new ChartPanel(chart);

        return panel;
    }
    
    /**
     * 
     * @param data
     * @param chartName
     * @param dataLabel
     * @param xAxis
     * @param yAxis
     * @return 
     */
    public static ChartPanel standardFunctionPlot(double[] data, String chartName,
            String dataLabel, String xAxis, String yAxis){
        XYSeries series1 = new XYSeries(dataLabel);
        for (int i = 0; i < data.length; i++) {
            series1.add(i, data[i]);
        }

        XYDataset plotData = new XYSeriesCollection(series1);

        JFreeChart chart = ChartFactory.createXYLineChart(chartName, xAxis, yAxis,
                plotData, PlotOrientation.VERTICAL, false,
                false, false);

        NumberAxis axisX = new NumberAxis(xAxis);
        axisX.setAutoRange(true);

        NumberAxis axisY = new NumberAxis(yAxis);
        axisY.setAutoRange(true);

        ChartPanel panel = new ChartPanel(chart);

        return panel;
    }

    /**
     * Creates a standard plot of a double valued data series, containing
     * multiple series (more than 1 line).
     * @param data
     * @param chartName
     * @param dataLabel
     * @param xAxis
     * @param yAxis
     * @return
     */
    public static ChartPanel multipleChannelStandardPlot(double[][] data,
            String chartName,String[] dataLabel, String xAxis, String yAxis) {
        XYSeriesCollection plotData = new XYSeriesCollection();
        for (int j = 0; j < data.length; j++) {
            XYSeries series1 = new XYSeries(dataLabel[j]);
            for (int i = 0; i < data[j].length; i++) {
                series1.add(i * 0.01, data[j][i]);
            }
            plotData.addSeries(series1);
        }

        JFreeChart chart = ChartFactory.createXYLineChart(chartName, xAxis, yAxis,
                plotData, PlotOrientation.VERTICAL, false,
                false, false);

        NumberAxis axisX = new NumberAxis(xAxis);
        axisX.setAutoRange(true);

        NumberAxis axisY = new NumberAxis(yAxis);
        axisY.setAutoRange(true);

        ChartPanel panel = new ChartPanel(chart);

        return panel;
    }

    /**
     * Creates a standard plot of a complex series, consisting of only 1 series.
     * @param data
     * @param dataLabel
     * @param xAxis
     * @param yAxis
     * @return
     */
    public static ChartPanel standardComplexXYPlot(Complex[] data, String dataLabel,
            String xAxis, String yAxis) {
        XYSeries data2 = new XYSeries(dataLabel);
        for (int i = 0; i < data.length; i++) {
            data2.add(data[i].getRealPart(), data[i].getImagPart());
        }

        XYDataset plotData = new XYSeriesCollection(data2);

        NumberAxis axisX = new NumberAxis(xAxis);
        axisX.setAutoRange(true);

        NumberAxis axisY = new NumberAxis(yAxis);
        axisY.setAutoRange(true);

        XYItemRenderer renderer = new StandardXYItemRenderer();
        XYPlot plot = new XYPlot(plotData, axisX, axisY, renderer);

        JFreeChart chart = new JFreeChart("Standard", plot);

        ChartPanel panel = new ChartPanel(chart);

        return panel;
    }

    /**
     * Creates a standard plot of a complex valued data series, containing
     * multiple series (more than 1 line).
     * @param data
     * @param dataLabel
     * @param xAxis
     * @param yAxis
     * @return
     */
    public static ChartPanel multipleChannelComplexXYPlot(Complex[][] data,
            String chartName,String[] dataLabel, String xAxis, String yAxis){
        XYSeriesCollection plotData = new XYSeriesCollection();
        for (int j = 0; j < data.length; j++) {
            XYSeries series1 = new XYSeries(dataLabel[j]);
            for (int i = 0; i < data[j].length; i++) {
                series1.add(data[j][i].getRealPart(), data[j][i].getImagPart());
            }
            plotData.addSeries(series1);
        }

        JFreeChart chart = ChartFactory.createXYLineChart(chartName, xAxis, yAxis,
                plotData, PlotOrientation.VERTICAL, false,
                false, false);

        NumberAxis axisX = new NumberAxis(xAxis);
        axisX.setAutoRange(true);

        NumberAxis axisY = new NumberAxis(yAxis);
        axisY.setAutoRange(true);

        ChartPanel panel = new ChartPanel(chart);

        return panel;
    }
}
